/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.Category;
import entity.CategoryFacadeLocal;
import entity.Product;
import entity.ProductFacadeLocal;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

/**
 *
 * @author SONY
 */
@Stateless
public class ProductService implements ProductServiceLocal {
    
    @EJB ProductFacadeLocal productFacadeLocal;
    @EJB CategoryFacadeLocal categoryFacadeLocal;

    @Override
    public List<Product> getAllProducts() {
        return productFacadeLocal.findAll();
    }

    @Override
    public Product getProductDetails_byPID(String pid) {
        return productFacadeLocal.find(pid);
    }

    @Override
    public List<Product> getAllProducts_byCategoryID(String cid) {
        List<Product> lstProducts_bycid = new ArrayList<Product>();
        try {
            List<Product> lstProducts = productFacadeLocal.findAll();
            for(int i = 0; i < lstProducts.size(); i++) {
                Category c = lstProducts.get(i).getCategoryID();
                if(c.getCategoryID().equals(cid)) {
                    Product p = lstProducts.get(i);
                    lstProducts_bycid.add(p);
                }
            }          
        } catch (Exception e) {
            System.err.println(e);
        }
        return lstProducts_bycid;
    }

    @Override
    public List<Product> getAllProducts_LikePName_byPrice(String pname, double minprice, double maxprice) {
        List<Product> lstProducts = new ArrayList<Product>();
        List<Product> lstProducts_find = productFacadeLocal.findAll_LikePName(pname);
        for(int i = 0; i < lstProducts_find.size(); i++) {
            Product product = lstProducts_find.get(i);
            double unitprice = product.getUnitPrice().doubleValue();
            if(unitprice >= minprice && unitprice < maxprice) {
                lstProducts.add(product);
            }
        }
        return lstProducts;
    }

    @Override
    public boolean checkQOH_byPID(String pid, int qty) {
        try {
            int QOH = productFacadeLocal.find(pid).getQoh();
            if(qty <= QOH) {
                return true;
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean updateQOH_byPID(String pid, int qty_new) {
        try {
            Product product = productFacadeLocal.find(pid);
            product.setQoh(qty_new);
            productFacadeLocal.edit(product);
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    } 

    @Override
    public List<Product> getAllProducts_LikePName(String pname) {
        return productFacadeLocal.findAll_LikePName(pname);
    }

    @Override
    public List<Product> getAllProducts_LikePID(String pid) {
        List<Product> lstProducts = new ArrayList<Product>();
        List<Product> lstProducts_find = productFacadeLocal.findAll();
        for(int i = 0; i < lstProducts_find.size(); i++) {
            Product product = lstProducts_find.get(i);
            if(product.getProductID().contains(pid)) {
                lstProducts.add(product);
            }
        }
        return lstProducts;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public boolean updateProduct(String pid, String pname, int qoh, BigDecimal unitprice, String cid) {
        try {
            Product product_edit = new Product();
            product_edit.setProductID(pid);
            product_edit.setProductName(pname);
            product_edit.setQoh(qoh);
            product_edit.setUnitPrice(unitprice);
            product_edit.setImageName(pid + ".jpg");
            Category c_edit = categoryFacadeLocal.find(cid);
            product_edit.setCategoryID(c_edit);
            productFacadeLocal.edit(product_edit);
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public boolean addProduct(String pid, String pname, int qoh, BigDecimal unitprice, String cid) {
        try {
            Product product_add = new Product();
            product_add.setProductID(pid);
            product_add.setProductName(pname);
            product_add.setQoh(qoh);
            product_add.setUnitPrice(unitprice);
            product_add.setImageName(pid + ".jpg");
            Category c = categoryFacadeLocal.find(cid);
            product_add.setCategoryID(c);
            productFacadeLocal.create(product_add);
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public boolean deleteProduct(String pid) {
        try {
            Product product_delete = productFacadeLocal.find(pid);
            productFacadeLocal.remove(product_delete);
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
}
